/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2020. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#ifndef _AC780X_TIMER_H
#define _AC780X_TIMER_H
/*!
 * @file ac780x_timer.h
 *
 * @brief This file provides timer module integration functions interfaces.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "ac7803x.h"

/* ============================================  Define  ============================================ */
#define ALLCHANNEL_INTERRUPT_FLAG       0xF
#define CHANNEL0_INTERRUPT_FLAG         0x1
#define CHANNEL1_INTERRUPT_FLAG         0x2
#define CHANNEL2_INTERRUPT_FLAG         0x4
#define CHANNEL3_INTERRUPT_FLAG         0x8

/*!
 * @brief TIMER channel instance index macro.
 */
#define TIMER_CHANNEL_INDEX(TIMERCHx)    ((uint8_t)(((uint32_t)(TIMERCHx) - TIMER_CHANNEL0_BASE) >> 4))

/* ===========================================  Typedef  ============================================ */
/*!
 * @brief timer clksource select
 */
typedef enum
{
    TIMER_CLKSR_APB =    0x00U,      /*!< Timer clksource is APB   */
    TIMER_CLKSR_HSI =    0x01U,      /*!< Timer clksource is HSI   */
    TIMER_CLKSR_HSE =    0x10U       /*!< Timer clksource is HSE   */
} TIMER_ClkSrcType;

/*!
 * @brief timer counter Mode
 */
typedef enum
{
    TIMER_WORKMODE_TIMER =           0x0U,      /*!< 32-bit Periodic Counter mode   */
    TIMER_WORKMODE_PULSECOUNTER =    0x1U       /*!< 32-bit Pulse Counter mode      */
} TIMER_ModeType;

/*!
 * @brief pulse captrue polarity select
 */
typedef enum
{
    PULSE_ACTIVE_HIGH =           0x0U,      /*!< Pulse polarity active high   */
    PULSE_ACTIVE_LOW =            0x1U       /*!< Pulse polarity active low    */
} TIMER_PulsePolarityType;

/*!
 * @brief Prescaler & filter Selection
 */
typedef enum
{
    TIMER_PRESCALE_2 =                        0x00U, /*!< Time mode: prescaler 2, Filter mode: invalid          */
    TIMER_PRESCALE_4_GLITCHFILTER_1 =         0x01U, /*!< Time mode: prescaler 4, Filter mode: 1 clocks         */
    TIMER_PRESCALE_8_GLITCHFILTER_3 =         0x02U, /*!< Time mode: prescaler 8, Filter mode: 3 clocks         */
    TIMER_PRESCALE_16_GLITCHFILTER_7 =        0x03U, /*!< Time mode: prescaler 16, Filter mode: 7 clocks        */
    TIMER_PRESCALE_32_GLITCHFILTER_15 =       0x04U, /*!< Time mode: prescaler 32, Filter mode: 15 clocks       */
    TIMER_PRESCALE_64_GLITCHFILTER_31 =       0x05U, /*!< Time mode: prescaler 64, Filter mode: 31 clocks       */
    TIMER_PRESCALE_128_GLITCHFILTER_63 =      0x06U, /*!< Time mode: prescaler 128, Filter mode: 63 clocks      */
    TIMER_PRESCALE_256_GLITCHFILTER_127 =     0x07U, /*!< Time mode: prescaler 256, Filter mode: 127 clocks     */
    TIMER_PRESCALE_512_GLITCHFILTER_255 =     0x08U, /*!< Time mode: prescaler 512, Filter mode: 255 clocks     */
    TIMER_PRESCALE_1024_GLITCHFILTER_511 =    0x09U, /*!< Time mode: prescaler 1024, Filter mode: 511 clocks    */
    TIMER_PRESCALE_2048_GLITCHFILTER_1023 =   0x0AU, /*!< Time mode: prescaler 2048, Filter mode: 1023 clocks   */
    TIMER_PRESCALE_4096_GLITCHFILTER_2047 =   0x0BU, /*!< Time mode: prescaler 4096, Filter mode: 2047 clocks   */
    TIMER_PRESCALE_8192_GLITCHFILTER_4095 =   0x0CU, /*!< Time mode: prescaler 8192, Filter mode: 4095 clocks   */
    TIMER_PRESCALE_16384_GLITCHFILTER_8191 =  0x0DU, /*!< Time mode: prescaler 16384, Filter mode: 8191 clocks  */
    TIMER_PRESCALE_32768_GLITCHFILTER_16383 = 0x0EU, /*!< Time mode: prescaler 32768, Filter mode: 16383 clocks */
    TIMER_PRESCALE_65536_GLITCHFILTER_32767 = 0x0FU  /*!< Time mode: prescaler 65536, Filter mode: 32767 clocks */
} TIMER_PrescalerType;

/*!
 * @brief timer cval read presicion sel
 */
typedef enum
{
    TIMER_BCLK_FCLK_4 = 0x00U,      /*!< BUSCLK/FUNCTION CLK >= 4        */
    TIMER_BCLK_FCLK_3 = 0x01U,      /*!< BUSCLK/FUNCTION CLK >= 3        */
    TIMER_BCLK_FCLK_1 = 0x02U       /*!< BUSCLK/FUNCTION CLK >= 1        */
} TIMER_Sel;

/*!
 * @brief TIMER channel configuration structure.
 */
typedef struct
{
    TIMER_ModeType timeMode;        /*!< TIMER channel mode set */
    TIMER_PulsePolarityType pinPol; /*!< TIMER channel pin pulse polarity selection */
    ACTION_Type chainModeEn;        /*!< TIMER channel chainmode enable */
    ACTION_Type bypEn;              /*!< TIMER channel bypass enable */
    TIMER_PrescalerType prescaler;  /*!< TIMER channel prescaler & filter selection*/
    TIMER_Sel cvalReadSel;          /*!< TIMER channel read cval precision selection*/
    ACTION_Type interruptEn;        /*!< TIMER channel interrupt enable */
    uint32_t periodValue;           /*!< TIMER channel period value */
    DeviceCallback_Type callback;   /*!< TIMER channel callback pointer */
} TIMER_ConfigType;

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */
/*!
 * @brief TIMER module initialize.
 *
 * @param[in] timekclk:select timer clk source
 * @return none
 */
void TIMER_Init(TIMER_ClkSrcType timerclk);

/*!
 * @brief TIMER channel initialize.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @param[in] config: pointer to configuration structure
 * @return none
 */
void TIMER_InitChannel(TIMER_CHANNEL_Type *TIMERCHx, const TIMER_ConfigType *config);

/*!
 * @brief TIMER De-initialize module.
 *
 * close all channels at the same time
 *
 * @param[in] none
 * @return none
 */
void TIMER_DeInit(void);

/*!
 * @brief TIMER channel De-initialize.
 *
 * only close single channel
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @return none
 */
void TIMER_DeInitChannel(TIMER_CHANNEL_Type *TIMERCHx);

/*!
 * @brief Set timer callback function.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 *            func: callback function
 * @return none
 */
void TIMER_SetCallback(TIMER_CHANNEL_Type *TIMERCHx, const DeviceCallback_Type func);

#ifdef __cplusplus
}
#endif

#endif /* _AC780X_TIMER_H */

/* =============================================  EOF  ============================================== */
